using System;
using System.Data;
using Deliveryroomstatusbulletin.EntityData;
using Skynet.Data;
using Skynet.ExceptionManagement;
using SystemFramework.NewCommon;

namespace Deliveryroomstatusbulletin.EntityDAO
{
    //*****************************************************************************
    //  文件名 DeliveryroomstatusbulletinDAO.cs
    //*****************************************************************************
    /// <summary>
    /// 数据库表 H_DELIVERYROOMSTATUSBULLETIN DAO层
    /// 主键 INHOSID
    /// </summary>
    /// <remark>
    /// 该表有7个字段
    /// 编制：简易代码生成器
    /// 时间：2013-03-15 09:32:38
    /// </remark>
    public class DeliveryroomstatusbulletinDAO
    {
        private IDataAccess data;
        
        public DeliveryroomstatusbulletinDAO(IDataAccess data)
        {
            this.data=data;
        }
        
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        private DeliveryroomstatusbulletinData GetData(IDataReader reader)
        {
            return GetData(reader, null);
        }
        
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        private DeliveryroomstatusbulletinData GetData(IDataReader reader, Action<IDataReader, DeliveryroomstatusbulletinData> action)
        {
            DeliveryroomstatusbulletinData entity = new DeliveryroomstatusbulletinData();

            if (reader["ALERTSTATUS"] != DBNull.Value)
            {
                entity.Alertstatus =
                    Convert.ToInt32(reader["ALERTSTATUS"]);
            }

            if (reader["BEDID"] != DBNull.Value)
            {
                entity.Bedid =
                    reader["BEDID"].ToString();
            }

            if (reader["STATEcaption1"] != DBNull.Value) 
            {
                entity.STATEcaption1 =
                    reader["STATEcaption1"].ToString();
            }
            if (reader["STATEcaption2"] != DBNull.Value)
            {
                entity.STATEcaption2 =
                    reader["STATEcaption2"].ToString();
            }
            if (reader["STATEcaption3"] != DBNull.Value)
            {
                entity.STATEcaption3 =
                    reader["STATEcaption3"].ToString();
            }
            //if (reader["CALLSTATUS"] != DBNull.Value)
            //{
            //    entity.Callstatus =
            //        Convert.ToInt32(reader["CALLSTATUS"]);
            //}

            if (reader["CHILDBIRTHSTATE"] != DBNull.Value)
            {
                entity.Childbirthstate =
                    Convert.ToInt32(reader["CHILDBIRTHSTATE"]);
            }

            if (reader["GOODS"] != DBNull.Value)
            {
                entity.Goods =
                    reader["GOODS"].ToString();
            }

            if (reader["INHOSID"] != DBNull.Value)
            {
                entity.Inhosid =
                    reader["INHOSID"].ToString();
            }

            if (reader["NAME"] != DBNull.Value)
            {
                entity.Name =
                    reader["NAME"].ToString();
            }

            if (action != null)
            {
                action(reader, entity);
            }

            entity.AcceptChange();

            return entity;
        }
        
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(DeliveryroomstatusbulletinData entity)
        {
            try
            {
                string sqlText = "INSERT INTO H_DELIVERYROOMSTATUSBULLETIN (ALERTSTATUS,BEDID,STATEcaption1,STATEcaption2,STATEcaption3,CHILDBIRTHSTATE,GOODS,INHOSID,NAME) " +
                    "VALUES (@ALERTSTATUS,@BEDID,@STATEcaption1,@STATEcaption2,@STATEcaption3,@CHILDBIRTHSTATE,@GOODS,@INHOSID,@NAME)";
                QueryParameterCollection para = new QueryParameterCollection();
                para.Add("@ALERTSTATUS", entity.Alertstatus);
                para.Add("@BEDID", entity.Bedid);
                para.Add("@STATEcaption1", entity.STATEcaption1);
                para.Add("@STATEcaption2", entity.STATEcaption2);
                para.Add("@STATEcaption3", entity.STATEcaption3);
            //    para.Add("@CALLSTATUS", entity.Callstatus);
                para.Add("@CHILDBIRTHSTATE", entity.Childbirthstate);
                para.Add("@GOODS", entity.Goods);
                para.Add("@INHOSID", entity.Inhosid);
                para.Add("@NAME", entity.Name);
                return data.ExecuteNonQuery(sqlText, para);
            }
            catch (Exception ex)
            {
                throw new LogonException(ex.Message, ex);
            }
        }
        
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(DeliveryroomstatusbulletinData entity)
        {
            try
            {
                string sqlText = "UPDATE H_DELIVERYROOMSTATUSBULLETIN SET " +
                    "ALERTSTATUS=@ALERTSTATUS,BEDID=@BEDID,STATEcaption1=@STATEcaption1,STATEcaption2=@STATEcaption2,STATEcaption3=@STATEcaption3,CHILDBIRTHSTATE=@CHILDBIRTHSTATE,GOODS=@GOODS,NAME=@NAME " +
                    "WHERE INHOSID=@INHOSID";
                QueryParameterCollection para = new QueryParameterCollection();
                para.Add("@ALERTSTATUS", entity.Alertstatus);
                para.Add("@BEDID", entity.Bedid);
                para.Add("@STATEcaption1", entity.STATEcaption1);
                para.Add("@STATEcaption2", entity.STATEcaption2);
                para.Add("@STATEcaption3", entity.STATEcaption3);
               // para.Add("@CALLSTATUS", entity.Callstatus);
                para.Add("@CHILDBIRTHSTATE", entity.Childbirthstate);
                para.Add("@GOODS", entity.Goods);
                para.Add("@NAME", entity.Name);
                para.Add("@INHOSID", entity.Inhosid);
                return data.ExecuteNonQuery(sqlText, para);
            }
            catch (Exception ex)
            {
                throw new LogonException(ex.Message, ex);
            }
        }
        
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="inhosid"></param>
        /// <returns></returns>
        public int Delete(string inhosid)
        {
            try
            {
                string sqlText = "DELETE FROM H_DELIVERYROOMSTATUSBULLETIN " +
                    "WHERE INHOSID=@INHOSID";

                QueryParameterCollection para = new QueryParameterCollection();

                para.Add("@INHOSID", inhosid);

                return data.ExecuteNonQuery(sqlText, para);
            }
            catch (Exception ex)
            {
                throw new LogonException(ex.Message, ex);
            }
        }
        
        /// <summary>
        /// 获取全部
        /// </summary>
        /// <returns></returns>
        public EntityList<DeliveryroomstatusbulletinData> GetAll()
        {
            return GetAll(null, null);
        }

        /// <summary>
        /// 获取全部
        /// </summary>
        /// <param name="isOrderBy">第二参数的类型 排序条件或兼容数据库TSQL语句</param>
        /// <param name="context">内容</param>
        /// <returns></returns>
        public EntityList<DeliveryroomstatusbulletinData> GetAll(bool isOrderBy, string context)
        {
            if (isOrderBy)
            {
                return GetAll(context, null);
            }
            else
            {
                return GetAll(null, context);
            }
        }
        
        public EntityList<DeliveryroomstatusbulletinData> GetAll(string orderBy, string sqlText)
        {
            try
            {
                EntityList<DeliveryroomstatusbulletinData> list = new EntityList<DeliveryroomstatusbulletinData>();

                orderBy = string.IsNullOrEmpty(orderBy) ? string.Empty : " ORDER BY " + orderBy;
                sqlText = string.IsNullOrEmpty(sqlText) ? "SELECT ALERTSTATUS,BEDID,STATEcaption1,STATEcaption2,STATEcaption3,CHILDBIRTHSTATE,GOODS,INHOSID,NAME " +
                    "FROM H_DELIVERYROOMSTATUSBULLETIN" + orderBy
                    : sqlText + orderBy;

                using (IDataReader reader = data.ExecuteReader(sqlText))
                {
                    while (reader.Read())
                    {
                        DeliveryroomstatusbulletinData entity = GetData(reader);
                        list.Add(entity);
                    }
                }

                return list;
            }
            catch (Exception ex)
            {
                throw new LogonException(ex.Message, ex);
            }
        }
        
        public EntityList<DeliveryroomstatusbulletinData> Get(string whereClause)
        {
            return Get(whereClause, null, null);
        }

        public EntityList<DeliveryroomstatusbulletinData> Get(string whereClause, bool isOrderBy, string context)
        {
            if (isOrderBy)
            {
                return Get(whereClause, context, null);
            }
            else
            {
                return Get(whereClause, null, context);
            }
        }
        
        public EntityList<DeliveryroomstatusbulletinData> Get(string whereClause, string orderBy, string sqlText)
        {
            try
            {
                EntityList<DeliveryroomstatusbulletinData> list = new EntityList<DeliveryroomstatusbulletinData>();

                whereClause = string.IsNullOrEmpty(whereClause) ? string.Empty : " WHERE " + whereClause;
                orderBy = string.IsNullOrEmpty(orderBy) ? string.Empty : " ORDER BY " + orderBy;
                sqlText = string.IsNullOrEmpty(sqlText) ? "SELECT ALERTSTATUS,BEDID,STATEcaption1,STATEcaption2,STATEcaption3,CHILDBIRTHSTATE,GOODS,INHOSID,NAME " +
                    "FROM H_DELIVERYROOMSTATUSBULLETIN" + whereClause + orderBy
                    : sqlText + whereClause + orderBy;

                using (IDataReader reader = data.ExecuteReader(sqlText))
                {
                    while (reader.Read())
                    {
                        DeliveryroomstatusbulletinData entity = GetData(reader);
                        list.Add(entity);
                    }
                }

                return list;
            }
            catch (Exception ex)
            {
                throw new LogonException(ex.Message, ex);
            }
        }

        //获取病房待产病人
        public EntityList<DeliveryroomstatusbulletinData> GetWaits()
        {
            string sqlText = " SELECT T.INHOSID,T.OFFICEBEDID as BEDID,t.PATIENTNAME as name,'' as GOODS,0 as CHILDBIRTHSTATE,'' as STATEcaption1,'' as STATEcaption2 ,'' as STATEcaption3,0 as ALERTSTATUS FROM ( " +
                                " SELECT distinct DR.*,TP.PATIENTNAME,H.OFFICEBEDID,TP.EMPLOYMENT,H.BALANCEMONEY,H.LINKMAN,H.RELATIONTELEPHONE," +
                                " TF.OFFICE,S.CHARGEKIND,H.ADVANCEMONEY,H.TOTALCONSUMEMONEY,H.BALANCEMONEY,TP.AGE,TP.TELEPHONE, " +
                                " TP.ADDRESS,TP.WORKUNIT,H.ENTERHOSDATE,H.CHARGEDOCTORID,ddr.FETALCHILDBIRTHTIME " +
                                "  FROM DR_ANTEPARTUMCHECKIN DR inner join H_INHOS_RECORD H on DR.INHOSID = H.INHOSID and DR.CURRENTINHOSMARK = H.CURRENTINHOSMARK" +
                                "  inner join T_PATIENT_INFO TP on H.DIAGNOSEID = TP.DIAGNOSEID " +
                                "  inner join T_OFFICE TF on  H.INHOSOFFICEID = TF.OFFICEID" +
                                "  inner join S_CHARGE_KIND S on  S.CHARGEKINDID = H.FEETYPEID" +
                                "  left join DR_deliveryRecord ddr on ddr.mothBh=H.INHOSID and H.CURRENTINHOSMARK=ddr.CURRENTINHOSMARK" +
                                "  WHERE DR.INVALIDORIGIN = '产房' ) T where not exists (select inhosid from H_DELIVERYROOMSTATUSBULLETIN where T.inhosid = H_DELIVERYROOMSTATUSBULLETIN.inhosid) " +
                                " union all " +
                                "  select INHOSID,BEDID,name,GOODS,CHILDBIRTHSTATE,STATEcaption1,STATEcaption2,STATEcaption3,ALERTSTATUS from H_DELIVERYROOMSTATUSBULLETIN ";
                                
                                

            try
            {
                EntityList<DeliveryroomstatusbulletinData> list = new EntityList<DeliveryroomstatusbulletinData>();
                using (IDataReader reader = data.ExecuteReader(sqlText))
                {
                    while (reader.Read())
                    {
                        DeliveryroomstatusbulletinData entity = GetData(reader);
                        list.Add(entity);
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                throw new LogonException(ex.Message, ex);
            }
        }
        
        public DeliveryroomstatusbulletinData GetByPrimaryKey(string inhosid)
        {
            return GetByPrimaryKey(inhosid, null);
        }

        public DeliveryroomstatusbulletinData GetByPrimaryKey(string inhosid, string sqlText)
        {
            try
            {
                DeliveryroomstatusbulletinData entity = null;

                sqlText = string.IsNullOrEmpty(sqlText) ? "SELECT ALERTSTATUS,BEDID,STATEcaption1,STATEcaption2,STATEcaption3,CHILDBIRTHSTATE,GOODS,INHOSID,NAME " +
                    "FROM H_DELIVERYROOMSTATUSBULLETIN " +
                    "WHERE INHOSID=@INHOSID"                
                    : sqlText;

                QueryParameterCollection para = new QueryParameterCollection();

                para.Add("@INHOSID", inhosid);

                using (IDataReader reader = data.ExecuteReader(sqlText, para))
                {
                    while (reader.Read())
                    {
                        entity = GetData(reader);
                    }
                }

                return entity;
            }
            catch (Exception ex)
            {
                throw new LogonException(ex.Message, ex);
            }
        }
    }
}
